<?php
/**
 * Created by PhpStorm.
 * User: mawenhuan
 * Date: 2018/1/2
 * Time: 下午1:18
 */

namespace App\Models\Meimei\Traits;


use App\Utils\OssUtil;

trait SignOssUrlTrait
{

	public function sign($keys=[])
	{
		foreach ($keys as $key)
		{
			if (isset($this->$key)) {
				$this->$key = OssUtil::fetchGetSignUrl($this->$key);
			}
		}
		return $this;
	}

	public function imgToPaths()
    {
        $paths = [];
        if (isset($this->imgs) && count($this->imgs) > 0){
            foreach ($this->imgs as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img);
            }
            $this->paths = $paths;
        }
        return $this;
    }

    public function signInfo()
    {
        $info_paths = [];
        foreach ($this->info as $info) {
            $info_paths[] = OssUtil::fetchGetSignUrl($info);
        }
        $this->infoPaths = $info_paths;
        return $this;
    }

    public function imgToPathsGarage()
    {
        $paths = [];
        if (isset($this->imgs) && count($this->imgs) > 0){
            foreach ($this->imgs as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img,true, 'image/resize,m_fixed,h_160,w_210');
            }
            $this->paths = $paths;
        }
        return $this;
    }

    public function iconToPaths()
    {
        $paths = [];
        if (isset($this->icon) && count($this->icon) > 0){
            foreach ($this->icon as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img,true, 'image/resize,m_fixed,h_160,w_210');
            }
            $this->iconPaths = $paths;
        }
        return $this;
    }

    public function iconCateToPath()
    {
        $this->icom_img = '';
        !empty($this->icon) && $this->icom_url = OssUtil::fetchGetSignUrl($this->icon,true, 'image/resize,m_fixed,h_200,w_200');
        return $this;
    }

    public function qualificationToPaths()
    {
        $paths = [];
        if (isset($this->qualification) && count($this->qualification) > 0){
            foreach ($this->qualification as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img,true);
            }
            $this->qualificationPaths = $paths;
        }
        return $this;
    }

    public function contractToPaths()
    {
        $paths = [];
        if (isset($this->contract) && count($this->contract) > 0){
            foreach ($this->contract as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img,true);
            }
            $this->contractPaths = $paths;
        }
        return $this;
    }

    public function imgToPathsMallGoods()
    {
        $paths = [];
        if (isset($this->imgs) && count($this->imgs) > 0){
            foreach ($this->imgs as $index => $img) {
                $paths[$index] = OssUtil::fetchGetSignUrl($img,true, 'image/resize,m_fixed,h_200,w_200');
            }
            $this->paths = $paths;
        }
        return $this;
    }

    public function copyPath()
    {
        if (isset($this->imgs) && isset($this->paths)){
            $this->imgs = $this->paths;
        }
        return $this;
    }

}
